----------------------------------------------------------------
-- Global Variables
----------------------------------------------------------------

EA_Window_EULAPopup = {}

----------------------------------------------------------------
-- Local Variables
----------------------------------------------------------------

----------------------------------------------------------------
-- EA_Window_EULAPopup Functions
----------------------------------------------------------------

function EA_Window_EULAPopup.Initialize()       

    -- if this is not an internal build and the EULA has cahanged then we will show the EULA
    if (PregameHasEULAChanged())
    then
        -- always show the EULA
        LabelSetText( "EA_Window_EULAPopupTitleBarText", GetPregameString( StringTables.Pregame.LABEL_EULA_TITLE))
        ButtonSetText("EA_Window_EULAPopupAcceptButton", GetPregameString( StringTables.Pregame.LABEL_PRESELECT_ACCEPT ))
        ButtonSetText("EA_Window_EULAPopupDeclineButton", GetPregameString( StringTables.Pregame.LABEL_PRESELECT_DECLINE ))
        LabelSetText( "EA_Window_EULAPopupAgreeCheckboxLabel", GetPregameString( StringTables.Pregame.LABEL_EULA_CHECKBOX))
        ButtonSetCheckButtonFlag("EA_Window_EULAPopupAgreeCheckbox", true)

        -- disable the accept button and the checkbox initially
        ButtonSetDisabledFlag("EA_Window_EULAPopupAcceptButton", true)

        EA_Window_EULAPopup.CreateTextBody()
    -- else if the ROC has changed we will show it
    elseif PregameHasROCChanged()
    then
        WindowSetShowing("EA_Window_EULA", false)
        if (DoesWindowExist("EA_Window_ROC")) then
            EA_Window_ROCPopup.Show()
        end
    -- otherwise we'll continue on our merry way
    else
        WindowSetShowing("EA_Window_EULA", false)
        
        SettingsWindowTabbed.DoLoginPerformanceWarning() 
    end
end

function EA_Window_EULAPopup.Shutdown()
    TextLogDestroy("EULALog")
end

function EA_Window_EULAPopup.Show()
    WindowSetShowing("EA_Window_EULA", true)
end

function EA_Window_EULAPopup.ToggleShowing()
    local showing = WindowGetShowing("EA_Window_EULA")
    WindowSetShowing("EA_Window_EULA", not showing)
end

function EA_Window_EULAPopup.Accept()
    -- don't do anything if the checkbox hasn't been checked
    if (ButtonGetDisabledFlag("EA_Window_EULAPopupAcceptButton"))
    then
        return
    end

    -- let the C side know that we have accepted the EULA
    BroadcastEvent( SystemData.Events.PREGAME_EULA_ACCEPTED )

    -- if the ROC has changed we will show it
    if PregameHasROCChanged()
    then
        if (DoesWindowExist("EA_Window_ROC")) then
            EA_Window_ROCPopup.Show()
        end
    -- if ROC has not changed we can do the performance warning
    -- otherwise it execute after ROC is accepted (see ROC lua)
    else
        SettingsWindowTabbed.DoLoginPerformanceWarning()
    end

    WindowSetShowing("EA_Window_EULA", false)
end

function EA_Window_EULAPopup.Decline()
    WindowSetShowing("EA_Window_EULA", false)
    -- if you don't accept then we will quit the game
    BroadcastEvent( SystemData.Events.QUIT )
end

function EA_Window_EULAPopup.CreateTextBody()
    TextLogDestroy("EULALog")
    TextLogCreate("EULALog", #StringTables.EULA)
    TextLogAddFilterType("EULALog", 1, L"")
    LogDisplayAddLog("EA_Window_EULAPopupLogDisplay", "EULALog", true)
    LogDisplaySetShowTimestamp("EA_Window_EULAPopupLogDisplay", false)
    LogDisplaySetShowLogName("EA_Window_EULAPopupLogDisplay", false)
    TextLogDisplayShowScrollbar("EA_Window_EULAPopupLogDisplay", true)

    for index = 0, #StringTables.EULA
    do
        TextLogAddEntry("EULALog", 1, GetStringFromTable("EULA", index))
    end

    LogDisplayScrollToTop("EA_Window_EULAPopupLogDisplay")
end

function EA_Window_EULAPopup.ToggleAgreeCheckbox()
    -- don't do anything if the checkbox hasn't been enabled
    if (ButtonGetDisabledFlag("EA_Window_EULAPopupAgreeCheckbox"))
    then
        return
    end

    local toggleIsPressed = ButtonGetPressedFlag("EA_Window_EULAPopupAgreeCheckbox")
    ButtonSetDisabledFlag("EA_Window_EULAPopupAcceptButton", not toggleIsPressed)
end

function EA_Window_EULAPopup.ToggleAgreeCheckboxFromLabel()
    -- don't do anything if the checkbox hasn't been enabled
    if (ButtonGetDisabledFlag("EA_Window_EULAPopupAgreeCheckbox"))
    then
        return
    end

    local toggleIsPressed = ButtonGetPressedFlag("EA_Window_EULAPopupAgreeCheckbox")
    ButtonSetPressedFlag("EA_Window_EULAPopupAgreeCheckbox", not toggleIsPressed)
    EA_Window_EULAPopup.ToggleAgreeCheckbox()
end

function EA_Window_EULAPopup.MouseOverCheckboxLabel()
    -- don't do anything if the checkbox hasn't been enabled
    if (ButtonGetDisabledFlag("EA_Window_EULAPopupAgreeCheckbox"))
    then
        return
    end

    LabelSetTextColor("EA_Window_EULAPopupAgreeCheckboxLabel", DefaultColor.YELLOW.r, DefaultColor.YELLOW.g, DefaultColor.YELLOW.b)
end

function EA_Window_EULAPopup.MouseOverEndCheckboxLabel()
    -- don't do anything if the checkbox hasn't been enabled
    if (ButtonGetDisabledFlag("EA_Window_EULAPopupAgreeCheckbox"))
    then
        return
    end

    LabelSetTextColor("EA_Window_EULAPopupAgreeCheckboxLabel", DefaultColor.WHITE.r, DefaultColor.WHITE.g, DefaultColor.WHITE.b)
end
